from django.db import models
from django.contrib.auth.models import User
# Create your models here.

# django会自动为模型生成主键.

class Board(models.Model):

    # 数据库列的最大长度和数据库级别字段的唯一性.
    name = models.CharField(max_length=30,unique=True)
    description = models.CharField(max_length=100)

    # __str__ 方法是对象的字符串表示形式。也就是通过属性objects可以访问模型管理器,模型管理器用于数据库操作.
    def __str__(self):
        return self.name

class Topic(models.Model):
    subject = models.CharField(max_length=255)
    last_updated = models.DateTimeField(auto_now_add=True)
    board = models.ForeignKey(Board,related_name='topics')
    starter = models.ForeignKey(User,related_name='topics')

class Post(models.Model):
    message = models.TextField(max_length=4000)
    topic = models.ForeignKey(Topic,related_name='posts')


    # 创建Post对象时为当前日期和时间.
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(null=True)

    # 模型之间的关系(外键关联),related_name 引用它关联的模型.
    created_by = models.ForeignKey(User,related_name='posts')
    updated_by = models.ForeignKey(User,null=True,related_name='+')

